package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.mine.ApplyShopModel;
import com.luck.easyfoodbatch.mvp.mine.ApplyShopPresenter;
import com.luck.easyfoodbatch.mvp.mine.ApplyShopView;
import com.luck.easyfoodbatch.mvp.model.ApplyShop;
import com.luck.easyfoodbatch.mvp.model.ShoperModel;
import com.luck.easyfoodbatch.pickerview.PopOneHelper;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopInfoT3Activity extends BaseActivity implements ApplyShopView {

    @BindView(R.id.iv_right_a)
    ImageView ivRightA;
    @BindView(R.id.tv_tab1)
    TextView tvTab1;
    @BindView(R.id.rl_tab1)
    RelativeLayout rlTab1;
    @BindView(R.id.iv_right_b)
    ImageView ivRightB;
    @BindView(R.id.tv_tab2)
    TextView tvTab2;
    @BindView(R.id.rl_tab2)
    RelativeLayout rlTab2;
    @BindView(R.id.iv_right_c)
    ImageView ivRightC;
    @BindView(R.id.tv_tab3)
    TextView tvTab3;
    @BindView(R.id.rl_tab3)
    RelativeLayout rlTab3;
    @BindView(R.id.iv_right_d)
    ImageView ivRightD;
    @BindView(R.id.tv_tab4)
    TextView tvTab4;
    @BindView(R.id.rl_tab4)
    RelativeLayout rlTab4;
    @BindView(R.id.cb_http1)
    CheckBox cbHttp1;
    @BindView(R.id.cb_http2)
    CheckBox cbHttp2;
    @BindView(R.id.btn_exchange)
    Button btnExchange;
    @BindView(R.id.tv_ydx_http)
    TextView tv_ydx_http;
    @BindView(R.id.tv_http)
    TextView tv_http;

    private PopOneHelper popOneHelper;
    private List<String> yes = new ArrayList<>();
    private int type = 0;
    private String seller_type = "", real_name = "", phone = "", shop_avatar = "",
            shop_name = "", shop_address = "", id_card = "", product_type = "", main_brand = "";
    private int real_goods = 1, send_ontime = 1, three_days_on = 1, is_agree_commission = 1;
    private ApplyShopModel applyShopModel;
    private int mode_type = 0;
    private ShoperModel.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_info_t3, "卖家认证3/3", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        mode_type = getIntent().getIntExtra("type", 0);
        if (mode_type == 1) {
            // 编辑模式
            data = (ShoperModel.DataBean) getIntent().getSerializableExtra("data");
            real_goods = data.getReal_goods();
            send_ontime = data.getSend_ontime();
            three_days_on = data.getThree_days_on();
            is_agree_commission = data.getIs_agree_commission();
            switch (real_goods) {
                case 1:
                    tvTab1.setText("是");
                    break;
                case 2:
                    tvTab1.setText("否");
                    break;
            }
            switch (send_ontime) {
                case 1:
                    tvTab2.setText("是");
                    break;
                case 2:
                    tvTab2.setText("否");
                    break;
            }
            switch (three_days_on) {
                case 1:
                    tvTab3.setText("是");
                    break;
                case 2:
                    tvTab3.setText("否");
                    break;
            }
            switch (is_agree_commission) {
                case 1:
                    tvTab4.setText("是");
                    break;
                case 2:
                    tvTab4.setText("否");
                    break;
            }
        }
        yes.add("是");
        yes.add("否");
        seller_type = getIntent().getStringExtra("seller_type");
        real_name = getIntent().getStringExtra("real_name");
        phone = getIntent().getStringExtra("phone");
        shop_avatar = getIntent().getStringExtra("shop_avatar");
        shop_name = getIntent().getStringExtra("shop_name");
        shop_address = getIntent().getStringExtra("shop_address");
        id_card = getIntent().getStringExtra("id_card");
        product_type = getIntent().getStringExtra("product_type");
        main_brand = getIntent().getStringExtra("main_brand");
        Tools.makeTextColor(cbHttp1, 7, cbHttp1.getText().length());
        Tools.makeTextColor(cbHttp2, 7, cbHttp2.getText().length());
        popOneHelper = new PopOneHelper(this);
        popOneHelper.setOnClickOkListener(new PopOneHelper.OnClickOkListener() {
            @Override
            public void onClickOk(int position, String str) {
                switch (type) {
                    case 0:
                        tvTab1.setText(str);
                        break;
                    case 1:
                        tvTab2.setText(str);
                        break;
                    case 2:
                        tvTab3.setText(str);
                        break;
                    case 3:
                        tvTab4.setText(str);
                        break;
                }
            }
        });

        cbHttp1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b && cbHttp2.isChecked()) {
                    btnExchange.setEnabled(true);
                    btnExchange.setBackgroundResource(R.drawable.shape_round_solid_bg);
                } else {
                    btnExchange.setEnabled(false);
                    btnExchange.setBackgroundResource(R.drawable.shape_round_solid_false_bg);
                }
            }
        });
        cbHttp2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b && cbHttp1.isChecked()) {
                    btnExchange.setEnabled(true);
                    btnExchange.setBackgroundResource(R.drawable.shape_round_solid_bg);
                } else {
                    btnExchange.setEnabled(false);
                    btnExchange.setBackgroundResource(R.drawable.shape_round_solid_false_bg);
                }
            }
        });
    }


    @OnClick({R.id.rl_tab1, R.id.rl_tab2, R.id.tv_http, R.id.tv_ydx_http, R.id.rl_tab3, R.id.rl_tab4, R.id.btn_exchange})
    public void onClick(View view) {
        popOneHelper.setListItem(yes);
        String tv_y1 = tvTab1.getText().toString().trim();
        String tv_y2 = tvTab2.getText().toString().trim();
        String tv_y3 = tvTab3.getText().toString().trim();
        String tv_y4 = tvTab4.getText().toString().trim();
        switch (view.getId()) {
            case R.id.tv_http:
                startAct(BaseHttpActivity.class);
                break;
            case R.id.tv_ydx_http:
                startAct(BaseYdxHttpActivity.class);
                break;
            case R.id.rl_tab1:
                //popOneHelper.show(view);
                type = 0;
                if (tv_y1.equals("是")) {
                    tvTab1.setText("否");
                    real_goods = 2;
                } else {
                    tvTab1.setText("是");
                    real_goods = 1;
                }
                break;
            case R.id.rl_tab2:
                //popOneHelper.show(view);
                type = 1;
                if (tv_y2.equals("是")) {
                    tvTab2.setText("否");
                    send_ontime = 2;
                } else {
                    tvTab2.setText("是");
                    send_ontime = 1;
                }
                break;
            case R.id.rl_tab3:
                //popOneHelper.show(view);
                type = 2;
                if (tv_y3.equals("是")) {
                    tvTab3.setText("否");
                    three_days_on = 2;
                } else {
                    tvTab3.setText("是");
                    three_days_on = 1;
                }
                break;
            case R.id.rl_tab4:
                //popOneHelper.show(view);
                type = 3;
                if (tv_y4.equals("是")) {
                    tvTab4.setText("否");
                    is_agree_commission = 2;
                } else {
                    tvTab4.setText("是");
                    is_agree_commission = 1;
                }
                break;
            case R.id.btn_exchange:
                showProgressDialog("正在提交，请稍候……");
                applyShopModel = new ApplyShopPresenter(ShopInfoT3Activity.this, mContext);
                applyShopModel.applyShop(seller_type, real_name, phone, shop_avatar, shop_name, shop_address, id_card, product_type, main_brand, real_goods + "", send_ontime + "", three_days_on + "", is_agree_commission + "");
                break;
        }

    }

    @Override
    public void applyShopSuccess(ApplyShop model) {
        if (model.getStatus() == HttpUtil.RESULT_OK) {
            T.showToast(mContext, model.getMsg());
            Intent intent = new Intent();
            setResult(RESULT_OK);
            finish();
        } else {
            T.showToast(mContext, model.getMsg());
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        }
    }
}
